<?php

namespace App\Jobs;

use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Str;

class SendXoForgotPassword extends Job
{
    protected $email;
    protected $userId;

    public function __construct($email, $userId)
    {
        $this->email = $email;
        $this->userId = $userId;
    }

    public function handle()
    {
        //发送密码确认链接
        $password = Str::random(16);

        $enPassword = Crypt::encrypt($password);
        $url = 'http://'.env('API_DOMAIN').'/api/password/confirm_reset';

        $resetArr = [
            'id' => $this->userId,
            'i' => base64_encode($enPassword),
        ];
        $url .= '?'.http_build_query($resetArr);
        \Log::debug('url: '.$url);

        $email = $this->email;
        $text = strtr(trans('email.forgot_content'), [':password' => $password, ':url' => $url]);
        app('mailer')->raw($text, function ($message) {
            $message->subject(trans('email.forgot_subject'));
            $message->to($this->email);
        });
    }
}
